<title>文章分类表</title>
<!--当前位置开始-->
<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <script type="text/html" template lay-done="layui.data.updateMainBreadcrumb();">
        </script>
    </div>
</div>
<!--当前位置结束-->
<style>
    /* 重写样式 */
</style>
<script type="text/html" template lay-type="Post" lay-url="Api/CoreCmsArticleType/GetIndex" lay-done="layui.data.done(d);">
    <div class="layui-form coreshop-search-form">
        <div class="layui-form-item">
            <div class="layui-inline">
                <button class="layui-btn  layui-btn-sm mybtn  layuiadmin-btn-admin" data-type="add"><i class="layui-icon layui-icon-add-1"></i>添加数据</button>
            </div>
        </div>
    </div>
</script>
<div class="table-body">
    <table id="LAY-app-CoreCmsArticleType-tableBox" lay-filter="LAY-app-CoreCmsArticleType-tableBox"></table>
</div>

<script type="text/html" id="LAY-app-CoreCmsArticleType-toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="addData"><i class="layui-icon layui-icon-add-1"></i>添加数据</button>
    </div>
</script>
<script type="text/html" id="LAY-app-CoreCmsArticleType-tableBox-bar">
    <!--<a class="layui-btn layui-btn-xs" lay-event="add">添加子分类</a>-->
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script>
    var indexData;
    var debug = layui.setter.debug;
    layui.data.done = function (d) {
        //开启调试情况下获取接口赋值数据
        if (debug) { console.log(d); }

        indexData = d.data;
        layui.use(['index', 'table', 'laydate', 'util', 'coreHelper', 'treeTable'],
            function () {
                var $ = layui.$
                    , admin = layui.admin
                    , table = layui.table
                    , form = layui.form
                    , laydate = layui.laydate
                    , setter = layui.setter
                    , coreHelper = layui.coreHelper
                    , util = layui.util
                    , treeTable = layui.treeTable
                    , view = layui.view;

                //重载form
                form.render();
                var searchwhere;

                //数据绑定
                var insTb = treeTable.render({
                    elem: '#LAY-app-CoreCmsArticleType-tableBox',
                    tree: {
                        iconIndex: 1,
                        idName: 'id',  // id的字段名
                        pidName: 'parentId',  // pid的字段名
                        openName: 'name',
                        isPidData: true
                    },
                    height: 'full-232',//无面包屑127,搜索框189,1行62
                    cols: [
                        { field: 'id', title: '序列', width: 60, sort: false },
                        { field: 'name', title: '分类名称', sort: false },
                        { field: 'sort', title: '排序 ', sort: false, width: 105 },
                        { width: 182, align: 'center', title: '操作', fixed: 'right', toolbar: '#LAY-app-CoreCmsArticleType-tableBox-bar' }
                    ],
                    reqData: function (data, callback) {
                        coreHelper.Post('Api/CoreCmsArticleType/GetPageList', null, function (res) {
                            if (res.code === 0) {
                                callback(res.data);
                            } else {
                                layer.msg(res.msg, { icon: 5, time: 2000 });
                            }
                        })
                    },
                    style: 'margin-top:0;'
                });
                //监听工具条
                treeTable.on('tool(LAY-app-CoreCmsArticleType-tableBox)',
                    function (obj) {
                        if (obj.event === 'del') {
                            doDelete(obj);
                        } else if (obj.event === 'edit') {
                            doEdit(obj)
                        } else if (obj.event === 'add') {
                            doAdd(obj.data.id)
                        }
                    });

                $('.layui-btn.layuiadmin-btn-admin').on('click', function () {
                    var type = $(this).data('type');
                    active[type] ? active[type].call(this) : '';
                });

                var active = {
                    add: function () {
                        doAdd(0);
                    }
                };

                //执行添加操作
                function doAdd(obj) {
                    coreHelper.Post("Api/CoreCmsArticleType/GetCreate", null, function (e) {
                        if (e.code === 0) {
                            admin.popup({ shadeClose: false,
                                title: '创建数据',
                                area: ['400px', '220px'],
                                id: 'LAY-popup-CoreCmsArticleType-create',
                                success: function (layero, index) {
                                    view(this.id).render('article/articletype/create', { data: e.data, id: obj }).done(function () {
                                        //监听提交
                                        form.on('submit(LAY-app-CoreCmsArticleType-createForm-submit)',
                                            function (data) {
                                                var field = data.field; //获取提交的字段
                                                if (debug) { console.log(field); } //开启调试返回数据
                                                //提交 Ajax 成功后，关闭当前弹层并重载表格
                                                coreHelper.Post("Api/CoreCmsArticleType/DoCreate", field, function (e) {
                                                    console.log(e)
                                                    if (e.code === 0) {
                                                        insTb.refresh();
                                                        layer.close(index); //再执行关闭
                                                        layer.msg(e.msg);
                                                    } else {
                                                        layer.msg(e.msg);
                                                    }
                                                });
                                            });
                                    });
                                }
                            });
                        } else {
                            layer.msg(e.msg);
                        }
                    });
                }

                //执行编辑操作
                function doEdit(obj) {
                    coreHelper.Post("Api/CoreCmsArticleType/GetEdit", { id: obj.data.id }, function (e) {
                        if (e.code === 0) {
                            admin.popup({ shadeClose: false,
                                title: '编辑数据',
                                area: ['400px', '220px'],
                                id: 'LAY-popup-CoreCmsArticleType-edit',
                                success: function (layero, index) {
                                    view(this.id).render('article/articletype/edit', { data: e.data }).done(function () {
                                        //监听提交
                                        form.on('submit(LAY-app-CoreCmsArticleType-editForm-submit)',
                                            function (data) {
                                                var field = data.field; //获取提交的字段
                                                if (debug) { console.log(field); } //开启调试返回数据
                                                //提交 Ajax 成功后，关闭当前弹层并重载表格
                                                coreHelper.Post("Api/CoreCmsArticleType/DoEdit", field, function (e) {
                                                    console.log(e)
                                                    if (e.code === 0) {
                                                        insTb.refresh();
                                                        layer.close(index); //再执行关闭
                                                        layer.msg(e.msg);
                                                    } else {
                                                        layer.msg(e.msg);
                                                    }
                                                });
                                            });
                                    })
                                }
                            });
                        } else {
                            layer.msg(e.msg);
                        }
                    });
                }
                //执行单个删除
                function doDelete(obj) {
                    layer.confirm('确定删除吗？删除后将无法恢复。', function (index) {
                        coreHelper.Post("Api/CoreCmsArticleType/DoDelete", { id: obj.data.id }, function (e) {
                            if (debug) { console.log(e); } //开启调试返回数据
                            insTb.refresh();
                            layer.msg(e.msg);
                        });
                    });
                }
                //监听 表格复选框操作
            });
    };
</script>